import {nanoid} from 'nanoid'
import {cloud} from 'boot/weixin'
import {formatConfig, getGameConfig, saveGameConfig} from "src/utils/config";

export async function getProject(id) {

}

export async function getProjectByCode(code) {

}

export async function uploadCloudFile(file) {
  // console.log(file)
  let local_filename = file.name
  let index = local_filename.lastIndexOf('.')
  let ext = local_filename.substr(index)
  let remote_filename = "fengxun-upload/" + nanoid() + ext
  // console.log('remote filename', remote_filename)

  try {
    return await cloud.uploadFile({
      cloudPath: remote_filename, // 云存储的路径
      filePath: file     // 需要上传的文件，File 类型
    })
  } catch (e) {
    return false
  }

}

export async function createCodePage() {
  const db = cloud.database()
  const allowWord = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
  const wordTable = allowWord.split('')
  const wordLength = allowWord.length

  for (let index = 0; index < 3; index++) {
    let pageLine = ''
    for (let row = 0; row < 22; row++) {
      let rowline = ''
      for (let col = 0; col < 50; col++) {
        rowline += wordTable[Math.floor(Math.random() * wordLength)]
      }
      pageLine += rowline + '\n'
    }
    // console.log(pageLine)
    await db.collection('book').add({
      content: pageLine,
      page: index,
    })
  }
}
